#!/bin/bash
# Finder app and source-package entry point. Uses only built-in macOS tools.
set -euo pipefail
export PATH=/usr/bin:/bin:/usr/sbin:/sbin
if [ "$#" -eq 2 ]; then
  source_dir="$1"; resource_dir="$2"
else
  resource_dir="$(cd -- "$(dirname -- "$0")/../Resources" && pwd)"
  source_dir="$resource_dir/extension"
fi
target_parent="$HOME/Library/Application Support/Adobe/CEP/extensions"
target="$target_parent/com.hcparty.ae"
data_dir="$HOME/Library/Application Support/HCParty-CEP"
backup=""
stage=""
lock=""
moved_old=0
moved_cache=0
installed=0
committed=0
pref11=""
pref12=""
prefs_changed=0
show_error() {
  /usr/bin/osascript - "$1" <<'APPLESCRIPT' || true
on run argv
  display dialog (item 1 of argv) with title "3HC" buttons {"OK"} default button "OK" with icon caution
end run
APPLESCRIPT
}
cleanup() {
  result=$?
  trap - EXIT
  if [ "$committed" -eq 0 ]; then
    set +e
    if [ "$prefs_changed" -eq 1 ]; then
      if [ -n "$pref11" ]; then defaults write com.adobe.CSXS.11 PlayerDebugMode -string "$pref11"; else defaults delete com.adobe.CSXS.11 PlayerDebugMode; fi
      if [ -n "$pref12" ]; then defaults write com.adobe.CSXS.12 PlayerDebugMode -string "$pref12"; else defaults delete com.adobe.CSXS.12 PlayerDebugMode; fi
    fi
    if [ "$installed" -eq 1 ]; then /bin/rm -rf "$target"; fi
    if [ "$moved_old" -eq 1 ]; then /bin/mv "$backup/extension" "$target"; fi
    if [ "$moved_cache" -eq 1 ]; then /bin/mv "$backup/updates" "$data_dir/updates"; fi
    if [ "$result" -ne 0 ]; then show_error "3HC couldn't finish installing. Close After Effects and try again. Previous files, if any, are kept in $backup. Help: https://3hc.app/download"; fi
  fi
  if [ -n "$stage" ]; then /bin/rm -rf "$stage"; fi
  if [ -n "$lock" ]; then /bin/rmdir "$lock"; fi
  exit "$result"
}
if [ "$(uname -s)" != Darwin ]; then echo 'This installer runs on macOS.' >&2; exit 1; fi
if ! /usr/bin/osascript <<'APPLESCRIPT'
display dialog "Close After Effects, then install.\n\nCloud connection and automatic updates are included. Installs for your user and enables unsigned Adobe CEP panels." with title "Install 3HC" buttons {"Cancel", "Install"} default button "Install" cancel button "Cancel"
APPLESCRIPT
then exit 0; fi
if /usr/bin/pgrep -x 'After Effects' >/dev/null || /usr/bin/pgrep -x 'After Effects Beta' >/dev/null; then show_error 'Close After Effects, then open Install 3HC again.'; exit 1; fi
case "$target" in *'#'*) show_error 'Adobe CEP does not support # in a user folder path. Use a Mac account without # in its folder name.'; exit 1;; esac
/bin/mkdir -p "$target_parent" "$data_dir"
if ! /bin/mkdir "$data_dir/installer.lockdir" 2>/dev/null; then show_error 'Another 3HC install may be running. Close it before trying again. If an install was interrupted, see 3hc.app/download#help.'; exit 1; fi
lock="$data_dir/installer.lockdir"
trap cleanup EXIT
backup="$data_dir/installer-backups/$(/usr/bin/uuidgen)"
stage="$(/usr/bin/mktemp -d "$target_parent/3hc-install.XXXXXX")"
count=0
while read -r hash name; do
  case "$hash" in *[!a-f0-9]*|'') exit 1;; esac
  [ "${#hash}" -eq 64 ]
  case "$name" in /*|*'..'*|*[!A-Za-z0-9_./-]*|'') exit 1;; esac
  /bin/mkdir -p "$stage/$(dirname -- "$name")"
  /bin/cp "$source_dir/$name" "$stage/$name"
  count=$((count+1))
done < "$resource_dir/files.sha256"
[ "$count" -ge 40 ]
(cd -- "$stage" && /usr/bin/shasum -a 256 -c "$resource_dir/files.sha256" >/dev/null)
if /usr/bin/pgrep -x 'After Effects' >/dev/null || /usr/bin/pgrep -x 'After Effects Beta' >/dev/null; then show_error 'Close After Effects before installing 3HC.'; exit 1; fi
pref11="$(defaults read com.adobe.CSXS.11 PlayerDebugMode 2>/dev/null || true)"
pref12="$(defaults read com.adobe.CSXS.12 PlayerDebugMode 2>/dev/null || true)"
/bin/mkdir -p "$backup"
if [ -e "$target" ]; then /bin/mv "$target" "$backup/extension"; moved_old=1; fi
if [ -e "$data_dir/updates" ]; then /bin/mv "$data_dir/updates" "$backup/updates"; moved_cache=1; fi
/bin/mv "$stage" "$target"
stage=""
installed=1
prefs_changed=1
defaults write com.adobe.CSXS.11 PlayerDebugMode -string 1
defaults write com.adobe.CSXS.12 PlayerDebugMode -string 1
committed=1
/usr/bin/osascript <<'APPLESCRIPT' || true
display dialog "Open After Effects.\n\nOnce: Settings > Scripting & Expressions > Allow Scripts to Write Files and Access Network.\n\nThen: Window > Extensions > 3HC." with title "3HC is installed" buttons {"Done"} default button "Done"
APPLESCRIPT
